﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERPClient.ServiceReferenceERP;

namespace ERPClient
{
    public partial class MainForm : Form
    {
        private int Token;
        private string Account;
        private ServiceClient Service = null;
        private int State = 0;
        private BOMQueryForm bomTable = null;
        private BOMCreateForm product = null;
        private InventoryForm inventory = null;
        private Form activeForm = null;

        public MainForm()
        {
            Token = 0;
            Account = "";
            
            InitializeComponent();

           

            Service = new ServiceClient();
            Service.Open();

            //Add
            //subForm = new SubFormYouWantToLoad();
            //subForm.TopLevel = false;
            //subForm.FormBorderStyle = FormBorderStyle.None;
            //ContainerPanel.Controls.Add(subForm, 0, 1);
            //subForm.Visible = true;


            //Remove
            //ContainerPanel.Controls.Remove(activeform);
            //activeform.Close();
            //activeform.Dispose();

        }

        ~MainForm()
        {
            if (Service != null) Service.Close();
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (State == 0)
            {
                Login login = new Login();
                DialogResult dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    Command cmd = Service.Login(login.userAccount, login.userPassword, "test");
                    if (cmd.Error == 0)
                    {
                        Account = login.userAccount;
                        Token = cmd.Token;
                        this.toolStripStatusLabel2.Text = string.Format("{0}<{1}>-登入成功", Account, Token);
                        State = 1;
                        loginToolStripMenuItem.Text = string.Format("登出-{0}", Account);
                    }
                }
            }
            else
            {
                State = 0;
                loginToolStripMenuItem.Text = "登入";
            }
        }

        private void InventoryMenuQuery_Click(object sender, EventArgs e)
        {
            if (activeForm != null)
            {
                ContainerPanel.Controls.Remove(activeForm);
            }
            if (bomTable == null)
            {
                bomTable = new BOMQueryForm();
                bomTable.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                bomTable.TopLevel = false;
                bomTable.Dock = DockStyle.Fill;
            }
            ContainerPanel.Controls.Add(bomTable);
            bomTable.Show();
            activeForm = bomTable;
        }

        private void InventoryMenuCreate_Click(object sender, EventArgs e)
        {
            if (activeForm != null)
            {
                ContainerPanel.Controls.Remove(activeForm);
            }
            if (product == null)
            {
                product = new BOMCreateForm();
                product.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                product.TopLevel = false;
                product.Dock = DockStyle.Fill;
            }
            ContainerPanel.Controls.Add(product);
            product.Show();
            activeForm = product;
        }

        private void InventoryItem_Click(object sender, EventArgs e)
        {
            if (activeForm != null)
            {
                ContainerPanel.Controls.Remove(activeForm);
            }
            if (product == null)
            {
                inventory = new InventoryForm();
                inventory.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                inventory.TopLevel = false;
                inventory.Dock = DockStyle.Fill;
            }
            ContainerPanel.Controls.Add(inventory);
            inventory.Show();
            activeForm = inventory;
        }      
    }
}
